# ------------------------------------------------------------------------------
# Base Detector Class
# ------------------------------------------------------------------------------
# 所有检测器的基类，提供统一接口
# ------------------------------------------------------------------------------

import cv2
import numpy as np
import time


class BaseDetector:
    """所有检测器的基类"""
    
    def __init__(self):
        self.fps = 0
        self.last_time = time.time()
        
    def update_fps(self):
        """更新 FPS 计数"""
        current_time = time.time()
        self.fps = 1.0 / (current_time - self.last_time) if (current_time - self.last_time) > 0 else 0
        self.last_time = current_time
        
    def visualize_fps(self, image):
        """在图像上显示 FPS"""
        if len(np.shape(image)) < 3:
            text_color = (255, 255, 255)  # white
        else:
            text_color = (0, 255, 0)  # green
            
        fps_text = 'FPS = {:.1f}'.format(self.fps)
        cv2.putText(image, fps_text, (24, 20), cv2.FONT_HERSHEY_PLAIN,
                    1, text_color, 1)
        return image
    
    def process_frame(self, frame):
        """
        处理单帧图像
        子类必须实现此方法
        
        Args:
            frame: OpenCV 格式的图像帧 (BGR)
            
        Returns:
            processed_frame: 处理后的图像帧
        """
        raise NotImplementedError("子类必须实现 process_frame 方法")
    
    def cleanup(self):
        """清理资源"""
        pass

